package com.zhou.message.service.impl;

import com.zhou.util.ObjectUtil;
import com.zhou.framework.util.MybatisPlusBatchUtil;
import com.zhou.message.dao.SendMessageAttach;
import com.zhou.message.mapper.SendMessageAttachMapper;
import com.zhou.message.service.SendMessageAttachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lang.zhou
 * @since 2023-03-01
 */
@Service
public class SendMessageAttachServiceImpl extends ServiceImpl<SendMessageAttachMapper, SendMessageAttach> implements SendMessageAttachService {

    @Override
    public Map<Long, List<Integer>> getMessageAttachIds(Collection<Long> sendIds) {
        Map<Long, List<Integer>> r = new HashMap<>();
        if(ObjectUtil.isNotEmpty(sendIds)){
            List<SendMessageAttach> batchList = MybatisPlusBatchUtil.batchList(this, SendMessageAttach::getSendId, sendIds);
            for (SendMessageAttach attach : batchList) {
                r.computeIfAbsent(attach.getSendId(),l-> new ArrayList<>(1)).add(attach.getAttachId());
            }
        }
        return r;
    }

    @Override
    public List<Integer> getMessageAttachId(Long sendId) {
        List<SendMessageAttach> batchList = MybatisPlusBatchUtil.batchList(this, SendMessageAttach::getSendId, sendId);
        return batchList.stream().map(SendMessageAttach::getAttachId).collect(Collectors.toList());
    }

    @Override
    public void addMessageAttach(Long sendId, Integer attachId) {
        SendMessageAttach attach = new SendMessageAttach();
        attach.setAttachId(attachId);
        attach.setSendId(sendId);
        save(attach);
    }

}
